"""
   ┌─┐       ┌─┐ + +
┌──┘ ┴───────┘ ┴──┐++
│                 │
│       ───       │++ + + +
███████───███████ │+
│                 │+
│       ─┴─       │
│                 │
└───┐         ┌───┘
    │         │
    │         │   + +
    │         │
    │         └──────────────┐
    │                        │
    │                        ├─┐
    │                        ┌─┘
    │                        │
    └─┐  ┐  ┌───────┬──┐  ┌──┘  + + + +
      │ ─┤ ─┤       │ ─┤ ─┤
      └──┴──┘       └──┴──┘  + + + +
             神兽保佑
            代码无BUG!
"""
import json
import base64
import time
import random

from Crypto.Cipher import AES
from Crypto.Util.Padding import pad
import requests
import execjs


class XueShu(object):
    def __init__(self):
        self.url = "https://www.termonline.cn/tm/api/tm/search"
        self.headers = {
            "Content-Type": "application/json",
            "Host": "www.termonline.cn",
            "htoken": "",
            "Origin": "https://www.termonline.cn",
            "Referer": "https://www.termonline.cn/search?k=%E5%BA%95%E7%BE%A4&r=1609808910005",
            "Sec-Fetch-Dest": "empty",
            "Sec-Fetch-Mode": "cors",
            "Sec-Fetch-Site": "same-origin",
            "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 11_1_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4356.6 Safari/537.36",
            "vtoken": "a04a638e08777fbb6047f356f815059936d5173612d0b1b1ab634ed47eefc054",
        }
        self.data = {"k": "", "highlight": "true", "pno": 1, "match": "1", "dbids": ["1"], "sdms": [], "gbnds": [],
                     "warn": 0, "uuid": "1609808910604"}

    def add_to_16(self, value):
        while len(value) % 16 != 0:
            value += '\0'
        return str.encode(value)  # 返回bytes

    # 加密方法
    def encrypt_oracle(self, text):
        # 秘钥
        key = '46cc793c53dc451b'
        # 待加密文本
        # 初始化加密器
        aes = AES.new(self.add_to_16(key), AES.MODE_ECB)
        # 先进行aes加密
        encrypt_aes = aes.encrypt(pad(text.encode("utf-8"), 16))
        # 用base64转成字符串形式
        encrypted_text = str(base64.encodebytes(encrypt_aes), encoding='utf-8')  # 执行加密并转码返回bytes
        return encrypted_text

    def get_json(self, key):
        time.sleep(random.randint(10, 30) / 10)
        self.data['k'] = key
        self.headers["htoken"] = self.encrypt_oracle(key).strip()
        json_ = requests.post(url=self.url, data=json.dumps(self.data), headers=self.headers,
                              allow_redirects=True).json()
        try:
            print(
                "名称：" + key + "\n",
                "英文名称：" + json_["data"]["agg"]["en"][0] + "\n",
                "相关学科：" + ",".join([i["ly"] for i in json_["data"]["agg"]["subs"]]) + "\n",
            )
        except:
            print(json_)


if __name__ == '__main__':
    xs = XueShu()
    for i in ["废气", "编程", "开始", "图书","内卷","废气","底群","延脑","座环","开花","a*","弃土","底面","弓脉","异型","底液","开口","开拍","引流","开销","廊道","建群","建造","弃管"]:
        xs.get_json(i)
